### 2021/3/17 10:54
# author:zkm
# github:zkm98
# brief: used for basic operating in project
## global variables used for recording the data
import os
times = "03171553"
# 返回的是一个列为单位的数据
def read_path_file(filename):
    filename = os.path.join("data", filename)
    if not os.path.exists(filename):
        return None
    files = open(filename)
    datas = files.readlines()
    response = []
    for line in datas:
        copes = line.strip().strip(',')
        res1 = [[float(x) for x in i.strip('[').strip(',').strip('[').split(',')] for i in copes.split(']')[:-1]]
        response.append(res1)
    return response

def read_pose_file(filename):
    filename = os.path.join("data", filename)
    if not os.path.exists(filename):
        return None
    files = open(filename)
    datas = files.readlines()
    return [[float(x) for x in line.strip().strip(',').strip('[').strip(']').split(',')]for line in datas]

def filter(data,k):
    ans = []
    sums = [0,0]
    for i in range(int(k/2)):
        ans.append(data[i])

    for i in range(k):
        sums[0] += data[i][0]
        sums[1] += data[i][1]
    for i in range(k,len(data)):
        temp = [0,0]
        temp[0] = sums[0] / k
        temp[1] = sums[1] / k
        ans.append(temp)
        sums[0] += data[i][0] - data[i-k][0]
        sums[1] += data[i][1] - data[i-k][1]
    temp = sums
    temp[0] /= k
    temp[1] /= k
    ans.append(temp)
    return ans

def get_distance_of_two_points(p1,p2):
    diff = np.abs(p1-p2)    
    dis = diff[0] + diff[1]
    # diff1 = diff*diff
    # dis = diff1[0] + diff1[1]
    return dis

